
from selenium.webdriver.common.by import By

from framework.basePage import BasePage
from page.model.searchModel import SearchModel

'''
	PO:高级搜索页面
'''

class HighSearchPage(BasePage):
	def __init__(self,driver = None):
		super().__init__(driver)

	# 搜索方式一：主题(必有) + 分类 + 话题 + 标签 (其它搜索属性可扩展)
	def titleCategoryTalkTagForHighSearch(self,searchModel:SearchModel):
		self.titleInput(searchModel.titleValue)
		if searchModel.categoryValue is not None:
			self.categoryChoice(searchModel.categoryValue)
		if searchModel.talkValue is not None:
			self.talkChoice(searchModel.talkValue)
		if searchModel.tagValue is not None:
			self.tagChoice(searchModel.tagValue)
		self.searchButton()
		#选择前n个标题存入list做断言，n取决于入参指定的断言数目
		titleList = []
		for i in range(1,searchModel.num+1):
			titleList.append(self.find(By.CSS_SELECTOR,f"div[class='fps-result-entries']>div:nth-child({i}) span[class='topic-title']>span").text)
		return titleList

	'''
		此页面的单个控件操作，用于场景搜索的组合
	'''
	# 主题搜索框
	def titleInput(self,value):
		self.sendKeys(By.CSS_SELECTOR, "[placeholder='搜索']", value)
		return self

	# 分类选择
	def categoryChoice(self,value):
		self.click(By.ID, 'search-in-category-header')
		self.sendKeys(By.CSS_SELECTOR, "[name='filter-input-search']", value)
		self.click(By.CSS_SELECTOR, f"[title={value}]")

	# 话题选择
	def talkChoice(self,value):
		self.click(By.CSS_SELECTOR,"div[class='control-group advanced-search-topic-status']>div[class='controls']>details[id='search-status-options']")
		self.click(By.CSS_SELECTOR, f"[title={value}]")

	# 标签选择
	def tagChoice(self,value):
		self.click(By.CSS_SELECTOR, "div[class='control-group advanced-search-tags']>div[class='controls']")
		self.sendKeys(By.CSS_SELECTOR, "[placeholder='可选标签']", value)
		self.click(By.CSS_SELECTOR, f"[title={value}]")

	# 搜索按钮
	def searchButton(self):
		self.click(By.CSS_SELECTOR,"button[aria-label='搜索']")





